var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var in_task = require("./inTask");
var point_map = require("./pointMap");
var READ_CONST_EVERY_ARTICLE_SWIPE_TIMES = 10;
var read = {
  go_page_from_home() {
    var logger = utils.create_logger("read go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["百度", "听书"], true)) {
      logger("");
      if (utils.click_text_starts_with("百度")) {
        logger("找到-" + utils.str_format("百度") + "-");
        sleep(utils.get_random_interval(2, 0, 1, 1000));
      } else {
        logger("没找到-" + utils.str_format("百度") + "-");
      }
      // if (utils.click_text_starts_with("热搜")) {
      //   logger("找到--热搜--");
      //   sleep(utils.get_random_interval(2, 0, 1, 1000));
      // } else {
      //   logger("没找到--热搜--");
      // }
      this.init();
    } else {
      logger("NOT FOUND");
      this.after();
    }
  },
  init() {
    var logger = utils.create_logger("read init: ");
    var content = ["听文章"];
    var ocr_text_results = ocr_utils.find_text_all(content, false);
    var length = ocr_text_results.length;
    if (length > 0) {
      logger("找到--" + length + "个--" + content.join("|||") + "--");
      var times = READ_CONST_EVERY_ARTICLE_SWIPE_TIMES;
      for (var i = 0; i < length; i++) {
        if (i == 1) {
          break;
        }
        var { result } = ocr_text_results[i];
        this.run(i, result, times);
      }
      this.after();
    } else {
      logger("没找到--" + content.join(" 或 ") + "--");
      this.after();
    }
  },
  check_is_over() {
    var logger = utils.create_logger("read check_is_over: ");
    var is_over_contents = ["立即评论", "没有更多", "抢沙发"];
    var ocr_text = ocr_utils.screen_exists_text_one(is_over_contents);
    if (ocr_text) {
      logger("找到-" + is_over_contents.join(" 或 ") + "-");
    }
    return ocr_text;
  },
  run(i, result, times) {
    var logger = utils.create_logger("read run: ");
    logger(
      "开始阅读第--" + (i + 1) + "--篇文章,找到的是--" + result.text + "--"
    );
    utils.click_x_y(device.width / 2, result.bounds.centerY());
    sleep(utils.get_random_interval(2, 0, 1, 1000));
    var swipe_times = times + random(0, 5);
    logger("将滑动--" + swipe_times + "--次");
    while (swipe_times > 0) {
      swipe(
        (device.width / 2) * utils.get_swipe_times(95, 5),
        (device.height / 10) * 8 * utils.get_swipe_times(95, 5),
        (device.width / 2) * utils.get_swipe_times(95, 5),
        (device.height / 10) * 1 * utils.get_swipe_times(95, 5),
        utils.get_random_interval(2, 0, 3, 100)
      );
      sleep(utils.get_random_interval(1, 0, 1, 1000));
      var is_over = this.check_is_over();
      if (is_over) {
        logger("判断阅读已经结束，将结束阅读");
        break;
      }
      sleep(utils.get_random_interval(1, 0, 1, 1000));
      swipe_times = swipe_times - 1;
    }
    back();
    sleep(utils.get_random_interval(2, 0, 1, 1000));
  },
  after() {
    console.log("read after");
    this.next();
  },
  next() {
    console.log("read next");
  },
};
module.exports = read;
